//
//  CYNContentViewController.m
//  App
//
//  Created by Cyandnow on 2019/1/25.
//  Copyright © 2019 NowStudio Co.,Ltd. All rights reserved.
//

#import <Masonry/View+MASAdditions.h>
#import <Flutter/Flutter.h>
#import "CYNContentViewController.h"
#import "CYNFlutterViewController.h"
#import "AppDelegate.h"

@interface CYNContentViewController () 

@property(nonatomic, retain) IBOutlet UIScrollView *scrollView;

@property(nonatomic, retain) UIView *contentView;

@property(nonatomic, retain) UITableViewController *tableViewController;

@property(nonatomic, retain) CYNFlutterViewController *flutterViewController;

@end

@implementation CYNContentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleFlutterScrollState:) name:@"com.yutong.app.flutter.scroll" object:nil];
    [self setupSubviews];
}

- (void)handleFlutterScrollState:(NSNotification *)notification {
    BOOL result = [notification.object boolValue];
    self.scrollView.scrollEnabled = !result;
}

- (void)setupSubviews {
    [self.scrollView addSubview:self.contentView];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.scrollView);
        make.height.mas_equalTo(self.scrollView);
    }];
    [self addChildViewController:self.tableViewController];
    [self.contentView addSubview:self.tableViewController.view];
    [self.tableViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.mas_equalTo(self.contentView);
        make.width.mas_equalTo(self.scrollView.mas_width);
    }];
    [self addChildViewController:self.flutterViewController];
    [self.contentView addSubview:self.flutterViewController.view];
    [self.flutterViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(self.contentView);
        make.left.mas_equalTo(self.tableViewController.view.mas_right);
        make.width.mas_equalTo(self.scrollView.mas_width);
    }];
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self.flutterViewController.view.mas_right);
    }];

}

- (UIView *)contentView {
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
    }
    return _contentView;
}

- (UITableViewController *)tableViewController {
    if (!_tableViewController) {
        _tableViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:nil] instantiateViewControllerWithIdentifier:@"CYNNativeTableViewController"];
    }
    return _tableViewController;
}

- (CYNFlutterViewController *)flutterViewController {
    if (!_flutterViewController) {
//        FlutterEngine *engine = [AppDelegate sharedAppDelegate].flutterEngine;
        _flutterViewController = [[CYNFlutterViewController alloc] init];
    }
    return _flutterViewController;
}






/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
